class Login{
    constructor(){
        this.user = document.getElementById("user");
        this.pass = document.getElementById("pass");
        this.submit = document.getElementById("submit");
        this.msg = document.querySelector(".msg");
        this.url = "http://api.icodeilife.cn:81/user";
        this.addEvent()
    }
    addEvent(){
        var that = this;
        this.submit.onclick = function(){
            that.u = that.user.value;
            that.p = that.pass.value;
            that.load();
        }
    }
    load(){
        ajax({
            url:this.url,
            data:{      
                type: "login",
                user: this.u,
                pass: this.p
            }
        }).then((res)=>{
            this.res = JSON.parse(res);
            this.display();
        })
    }
    display(){
        switch(this.res.code){
            case 0:
                this.msg.innerHTML = this.res.msg + "，3秒后到注册";
                setTimeout(() => {
                    location.href = "register.html";
                }, 3000);
                break;
            case 1:
                this.msg.innerHTML = "登录成功";
                setTimeout(() => {
                    location.href = "../index.html";
                }, 1000);

                sessionStorage.setItem("userMsg",JSON.stringify(this.res.msg));
                
                break;
            case 2:
                this.msg.innerHTML = this.res.msg + "，请重新输入";
                this.user.value = this.pass.value = "";
                this.user.focus();
        }
    }
}

new Login;